package com.gugee.tiktok.common.model.dal.ranking.impl;

import com.gugee.tiktok.common.model.dal.ranking.RankingExtRepository;
import com.gugee.tiktok.common.model.ranking.Ranking;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class RankingExtRepositoryImpl implements RankingExtRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void bathSave(List<Ranking> rankings) {
        long seconds = DateTimeUtils.currentTimeSecond();

        jdbcTemplate.batchUpdate("INSERT INTO " +
                "`tiktok`.`ranking`" +
                "(`blogger_type`, `ranking`, `ranking_type`, `blogger_id`, `uid`," +
                " `nickname`, `unique_id`, `avatar`, `state`, `position`, " +
                "`follower`,`score`, `likes_growth`,`review_growth`,`share_growth`,`view_growth`,`follower_growth`, `belong`, " +
                "`create_time`, `create_by`, `update_time`, `update_by`,`enterprise_verify_reason`,`custom_verify`," +
                "`region`,`likes`,`engagement_rate`," +
                "`avg_view`,`with_fusion_shop_entry`,`bio_url`,`bio_email`) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?,?)", new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                Ranking ranking = rankings.get(i);
                preparedStatement.setLong(1,ranking.getBloggerType());
                preparedStatement.setInt(2,ranking.getRanking());
                preparedStatement.setInt(3,ranking.getRankingType());
                preparedStatement.setLong(4,ranking.getBloggerId());
                preparedStatement.setLong(5,ranking.getUid());
                preparedStatement.setString(6,ranking.getNickname());
                preparedStatement.setString(7,ranking.getUniqueId());
                preparedStatement.setString(8,ranking.getAvatar());
                preparedStatement.setString(9,ranking.getState());
                preparedStatement.setInt(10,ranking.getPosition());
                preparedStatement.setInt(11,ranking.getFollower());
                preparedStatement.setDouble(12,ranking.getScore());
                preparedStatement.setLong(13,ranking.getLikesGrowth());
                preparedStatement.setLong(14,ranking.getReviewGrowth());
                preparedStatement.setLong(15,ranking.getShareGrowth());
                preparedStatement.setLong(16,ranking.getViewGrowth());
                preparedStatement.setLong(17,ranking.getFollowerGrowth());
                preparedStatement.setLong(18,ranking.getBelong());
                preparedStatement.setLong(19,seconds);
                preparedStatement.setString(20,ranking.getCreateBy());
                preparedStatement.setLong(21,seconds);
                preparedStatement.setString(22,ranking.getUpdateBy());
                preparedStatement.setString(23,ranking.getEnterpriseVerifyReason());
                preparedStatement.setString(24,ranking.getCustomVerify());
                preparedStatement.setString(25,ranking.getRegion());
                preparedStatement.setLong(26,ranking.getLikes());
                preparedStatement.setDouble(27,ranking.getEngagementRate());
                preparedStatement.setLong(28,ranking.getAvgView());
                preparedStatement.setBoolean(29,ranking.isWithFusionShopEntry());
                preparedStatement.setString(30,ranking.getBioUrl());
                preparedStatement.setString(31,ranking.getBioEmail());
            }

            @Override
            public int getBatchSize() {
                return rankings.size();
            }
        });
    }
}
